package com.billing.gwt.client;

import com.billing.gwt.client.data.BillingDatabase;
import com.billing.gwt.shared.BillingEntry;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionModel;

public class BillingEntryDataGrid {

	public static DataGrid<BillingEntry> create() {

		final DataGrid<BillingEntry> dataGrid = new DataGrid<BillingEntry>(BillingDatabase.get().getDataProvider());

		// Add selection model
		final SelectionModel<BillingEntry> selectionModel = new MultiSelectionModel<BillingEntry>();
		dataGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<BillingEntry> createCheckboxManager());

		// Create the columns
		createColumns(dataGrid, selectionModel);

		BillingDatabase.get().addDataDisplay(dataGrid);

		return dataGrid;
	}
	private static void createColumns(DataGrid<BillingEntry> dataGrid, final SelectionModel<BillingEntry> selectionModel){
		
		Column<BillingEntry, Boolean> checkColumn = new Column<BillingEntry, Boolean>(new CheckboxCell(true, false)) {
	        @Override
	        public Boolean getValue(BillingEntry object) {
	          // Get the value from the selection model.
	          return selectionModel.isSelected(object);
	        }
	      };
	  
		
		Column<BillingEntry, String> columnDescription = new Column<BillingEntry, String>(new EditTextCell()) {
			
			@Override
			public String getValue(BillingEntry object) {
				return object.getDescription();
			}
		};
		
		Column<BillingEntry, String> columnPayer = new Column<BillingEntry, String>(new EditTextCell()) {
			
			@Override
			public String getValue(BillingEntry object) {
				return object.getPayer();
			}
		};
		
		
	//	Column<BillingEntry, Number> columnId = new Column<BillingEntry, Number>(new NumberCell()) {
	//		
	//		@Override
	//		public Long getValue(BillingEntry object) {
	//			return object.getId();
	//		}
	//	};
		
		Column<BillingEntry, Number> columnAmount = new Column<BillingEntry, Number>(new NumberCell()) {
			
			@Override
			public Double getValue(BillingEntry object) {
				return object.getAmount();
			}
		};
		
		Column<BillingEntry, String> columnDate = new Column<BillingEntry, String>(new TextCell()) {
			
			@Override
			public String getValue(BillingEntry object) {
				if (object.getExecutionDate() != null){
					return DateTimeFormat.getShortDateFormat().format(object.getExecutionDate());
				}
				else {
					return "";
				}
			}
		};
		
		dataGrid.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
	//	dataGrid.addColumn(columnId,"#" );
		dataGrid.addColumn(columnDescription, "Descripcion" );
		dataGrid.addColumn(columnPayer, "Deudor");
		dataGrid.addColumn(columnAmount, "Cantidad");
		dataGrid.addColumn(columnDate, "Fecha");
		
	}


}
